#!/bin/bash

# 1, 检查版本文件，判断是否已经安装过dataxone，如果安装过的，则升级
# 检查方式： ~/.dataxone/config (不存在该文件时，不支持)
# ~/.dataxone/config
# DATAXONE_BASE:DATAXONE_VERSION:LOAD_BALANCE(HA MODE)
# /data/dataxone:4.3.6:N:
# /data/dataxone2:4.3.5:N:
#  ~/.dataxone/port
# DATAXONE_BASE:tomcat:dataXoneTomcat:regd:query4largedata:query4gauss:manager:yxad.mysql
# /data/dataxone:8080:8081:6000:8880:9011:9998:65534
#
# 2, 如果存在跨版本升级时，
# 需从patchset/<PATCH_VERSION>/sqlfiles/xxxxx，比如 4.3.4 -> 4.4.0，中间的sqlfiles需要执行
# 需要判断参数文件，一般来说，当前的全量版本参数是齐全的，所以需要通过旧参数文件->更新->新参数文件：最后输出定制化参数，判断是否需要修改
#
#
# -------------------------------
version=2.0.1
update=2024-11-25
SCRIPT_HOME_0=$(cd $(dirname "$0"); pwd)
SCRIPT_HOME=${SCRIPT_HOME:-$SCRIPT_HOME_0}
SCRIPT_NAME_0=$0
SCRIPT_NAME=$(basename $SCRIPT_NAME_0)
TMP_DIR=$SCRIPT_HOME/tmp
ARGV_PARAM_HOST_USER=${ARGV_PARAM_HOST_USER:-dataxone}
ARGV_PARAM_HOST_USER_PASSWORD=${ARGV_PARAM_HOST_USER_PASSWORD:-dataxone}

DEBUG=0
ARGV_PARAM_DATAXONE_INSTALL_DIR=${ARGV_PARAM_DATAXONE_INSTALL_DIR:-/data/dataxone}

usage(){
	echo "$SCRIPT_NAME Ver $version
DataXone interim Installer version $version, Updated on $update.

Usage: $SCRIPT_NAME COMMAND OPTIONS

Commands:
        install    First Install Dataxone Services
        uninstall  Uninstall Database Services
        patch      Upgrade Dataxone Services, gather than V4.3.6
        unpatch    Rollback to last Dataxone version.

Options:
        --dataxone-base=#        Dataxone Install Directory, default /data/dataxone
        --dataxone-portal=#      Dataxone Web Access Portal, Provide the VIP or domain, Such as: http://<dataxone-portal>:<tomcat-port>/DSGWEB/#/login
        --zookeeper-services=#   Zookeeper Services for Dataxone Manager
        --java-home=#            JAVA Home, If directory is empty, The built-in JDK will be installed
        --oracle-instant=#       Oracle Client Home, If directory is empty, The built-in Oracle Instant will be installed
        --db-host=#              MYSQL: The Dataxone Metadata Database, Hostname 
        --db-user=#              MYSQL: The Dataxone Metadata Database, Login Username, default dataxone
        --db-port=#              MYSQL: The Dataxone Metadata Database, Port, default 3306
        --db-password=#          MYSQL: The Dataxone Metadata Database, Login User Password
        --ips=ip1,ip2            Provide physical IP for all nodes, commonly 2 nodes, format: ip1,ip2,...
        --user=#                 Dataxone Os SSH Username, default dataxone
        --port=#                 Dataxone Os SSH port, default 22
        --user-password=#        Dataxone Os SSH User password
        --webapps=#              Dataxone Install services, format: webapps1,webapps2...
        --tomcat-port=#          Dataxone Tomcat Services Port, default 8080
	--dataxone-webapps=#     Dataxone Install services, format: webapps1,webapps2...
        --dataxone-tomcat-port=# Dataxone Worker Tomcat Services Port, default 8081
        -d|--debug) DEBUG=1 ;;	
	
"

}

argument_count=$#
[ $argument_count -eq 0 ] && usage && exit 1

command=$1
shift

print(){
	echo "$(date +"%Y-%m-%d %H:%M:%S") $1"
}
info(){
	echo "$(date +"%Y-%m-%d %H:%M:%S") [ INFO] $1"
}
err(){
	echo "$(date +"%Y-%m-%d %H:%M:%S") [ERROR] $1"
}
debug(){
	[ $DEBUG -eq 0 ] && return
	echo "$(date +"%Y-%m-%d %H:%M:%S") [DEBUG] $1"
}
abort(){
	echo "$SCRIPT_NAME aborted."
	exit -1
}

case $command in
	install|uninstall|patch|unpatch) ;;
	*) err "Unsupport command ‘$command’" && usage && abort ;;
esac



argument_count=`expr $argument_count - 1`
for i in $(seq 1 $argument_count); do
    arg="$1"
    shift
    # ...
    val=`echo "$arg" | sed -e 's;^--[^=]*=;;'`
    case "$arg" in
        --dataxone-base=*) ARGV_PARAM_DATAXONE_INSTALL_DIR="$val" ;;
        --dataxone-portal=*) ARGV_PARAM_DATAXONE_PORTAL="$val" ;;
        
        --zookeeper-services=*) ARGV_PARAM_ZOOKEEPER_SERVICES="$val" ;;
        --java-home=*) ARGV_PARAM_JAVA_HOME="$val" ;;
        --oracle-instant=*) ARGV_PARAM_JAVA_HOME="$val" ;;
        --db-host=*) ARGV_PARAM_DATAXONE_DB_HOST="$val" ;;
        --db-user=*) ARGV_PARAM_DATAXONE_DB_USER="$val" ;;
        --db-port=*) ARGV_PARAM_DATAXONE_DB_PORT="$val" ;;
        --db-password=*) ARGV_PARAM_DATAXONE_DB_USER_PASSWORD="$val" ;;
        --ips=*) ARGV_PARAM_OS_IPS="$val" ;;
        --user=*) ARGV_PARAM_OS_USER="$val" ;;
        --port=*) ARGV_PARAM_OS_PORT="$val" ;;
        --user-password=*) ARGV_PARAM_OS_USER_PASSWORD="$val" ;;
        --webapps=*) ARGV_PARAM_WEBAPPS="$val" ;;
        --tomcat-port=*) ARGV_PARAM_TOMCAT_PORT="$val" ;;
	--dataxone-webapps=*) ARGV_PARAM_DATAXONE_WEBAPPS="$val" ;;
        --dataxone-tomcat-port=*) ARGV_PARAM_DATAXONE_TOMCAT_PORT="$val" ;;

        -d|--debug) DEBUG=1 ;;
    esac
done

prelude(){
        debug "PRELUDE::COMMAND=$command"

        ARGV_PARAM_OS_USER=${ARGV_PARAM_OS_USER:-dataxone}
        ARGV_PARAM_OS_PORT=${ARGV_PARAM_OS_PORT:-22}
        debug "PRELUDE::ARGV_PARAM_OS_IPS=$ARGV_PARAM_OS_IPS"
        debug "PRELUDE::ARGV_PARAM_OS_USER=$ARGV_PARAM_OS_USER"
        debug "PRELUDE::ARGV_PARAM_OS_PORT=$ARGV_PARAM_OS_PORT"
        debug "PRELUDE::ARGV_PARAM_OS_USER_PASSWORD=$ARGV_PARAM_OS_USER_PASSWORD"

        ARGV_PARAM_DATAXONE_DB_USER=${ARGV_PARAM_DATAXONE_DB_USER:-dataxone}
        ARGV_PARAM_DATAXONE_DB_PORT=${ARGV_PARAM_DATAXONE_DB_PORT:-3306}
        debug "PRELUDE::ARGV_PARAM_DATAXONE_DB_HOST=$ARGV_PARAM_DATAXONE_DB_HOST"
        debug "PRELUDE::ARGV_PARAM_DATAXONE_DB_USER=$ARGV_PARAM_DATAXONE_DB_USER"
        debug "PRELUDE::ARGV_PARAM_DATAXONE_DB_PORT=$ARGV_PARAM_DATAXONE_DB_PORT"
        debug "PRELUDE::ARGV_PARAM_DATAXONE_DB_USER_PASSWORD=$ARGV_PARAM_DATAXONE_DB_USER_PASSWORD"

        ARGV_PARAM_DATAXONE_INSTALL_DIR=$(echo "$ARGV_PARAM_DATAXONE_INSTALL_DIR" | sed 's#//*#/#g' | awk '{sub(/\/$/, "", $0); print $0}')
        debug "PRELUDE::ARGV_PARAM_DATAXONE_INSTALL_DIR=$ARGV_PARAM_DATAXONE_INSTALL_DIR"
        
        debug "PRELUDE::ARGV_PARAM_DATAXONE_PORTAL=$ARGV_PARAM_DATAXONE_PORTAL"
        debug "PRELUDE::ARGV_PARAM_ZOOKEEPER_SERVICES=$ARGV_PARAM_ZOOKEEPER_SERVICES"
        debug "PRELUDE::ARGV_PARAM_JAVA_HOME=$ARGV_PARAM_JAVA_HOME"
        debug "PRELUDE::ARGV_PARAM_WEBAPPS=$ARGV_PARAM_WEBAPPS"
        debug "PRELUDE::ARGV_PARAM_DATAXONE_WEBAPPS=$ARGV_PARAM_DATAXONE_WEBAPPS"
        debug "PRELUDE::ARGV_PARAM_TOMCAT_PORT=$ARGV_PARAM_TOMCAT_PORT"
        debug "PRELUDE::ARGV_PARAM_DATAXONE_TOMCAT_PORT=$ARGV_PARAM_DATAXONE_TOMCAT_PORT"

        
        DATAXONE_LOCAL_CONFIG_HOME=${DATAXONE_LOCAL_CONFIG_HOME:-~/.dataxone}
        DATAXONE_LOCAL_CONFIG_FILE=$DATAXONE_LOCAL_CONFIG_HOME/config
        DATAXONE_LOCAL_PORT_FILE=$DATAXONE_LOCAL_CONFIG_HOME/port
        DATAXONE_LOCAL_WEBAPPS_FILE=$DATAXONE_LOCAL_CONFIG_HOME/webapps

        chmod +w $DATAXONE_LOCAL_CONFIG_FILE
        [ $? -ne 0 ] && echo "[WARN] Execute \"dataxone-fix.sh\" script and retry." && abort
        chmod +w $DATAXONE_LOCAL_PORT_FILE 
        [ $? -ne 0 ] && echo "[WARN] Execute \"dataxone-fix.sh\" script and retry." && abort
        chmod +w $DATAXONE_LOCAL_WEBAPPS_FILE
        [ $? -ne 0 ] && echo "[WARN] Execute \"dataxone-fix.sh\" script and retry." && abort

        debug "PRELUDE::DATAXONE_LOCAL_CONFIG_HOME=$DATAXONE_LOCAL_CONFIG_HOME"
        debug "PRELUDE::DATAXONE_LOCAL_CONFIG_FILE=$DATAXONE_LOCAL_CONFIG_FILE"
        debug "PRELUDE::DATAXONE_LOCAL_PORT_FILE=$DATAXONE_LOCAL_PORT_FILE"
        debug "PRELUDE::DATAXONE_LOCAL_WEBAPPS_FILE=$DATAXONE_LOCAL_WEBAPPS_FILE"


}

prelude

####
#### SSH Connection Test
ssh_test(){
        for ip in `echo $ARGV_PARAM_OS_IPS | tr ',' '\n'`
        do
                # yum install -y openssl openssl-devel
                # error while loading shared libraries: libssl.so.10: cannot open shared object file: No such file or directory
                debug "PRELUDE::test> $SCRIPT_HOME/sshcli -u${ARGV_PARAM_OS_USER} -pXXXXXX -P${ARGV_PARAM_OS_PORT} -h${ip} -tdate"
                $SCRIPT_HOME/sshcli -u${ARGV_PARAM_OS_USER} -p${ARGV_PARAM_OS_USER_PASSWORD} -P${ARGV_PARAM_OS_PORT} -h${ip} -tdate >/dev/null 2>&1
                [ $? -ne 0 ] && err "PRELUDE::SSH::[$ip:$ARGV_PARAM_OS_PORT] Connection failed" && abort
                info "Dataxone Node SSH [$ip:$ARGV_PARAM_OS_PORT] test OK."
        done
}
ssh_test

####
#### DB Connection Test
db_test(){
        export MYSQL_PWD="$ARGV_PARAM_DATAXONE_DB_USER_PASSWORD"
        debug "PRELUDE::test> mysql -h${ARGV_PARAM_DATAXONE_DB_HOST} -u${ARGV_PARAM_DATAXONE_DB_USER} -P${ARGV_PARAM_DATAXONE_DB_PORT}"
        mysql -h${ARGV_PARAM_DATAXONE_DB_HOST} -u${ARGV_PARAM_DATAXONE_DB_USER} -P${ARGV_PARAM_DATAXONE_DB_PORT} >/dev/null <<EOF
exit
EOF
        [ $? -ne 0 ] && err "PRELUDE::DB::[$ARGV_PARAM_DATAXONE_DB_HOST:$ARGV_PARAM_DATAXONE_DB_PORT] Connection failed" && abort
        info "Dataxone Metadata Database [$ARGV_PARAM_DATAXONE_DB_HOST:$ARGV_PARAM_DATAXONE_DB_PORT] test OK."
}
db_test



# Check Directory is Install
#########################################
handleUserDirectory(){
        dir="$1"

        dir_prefix=$(echo $dir | awk '{print $1}')

        if [ "${dir_prefix:0:1}" != "/" ]; then
                (>&2 err "Relative path should not be entered")
                return 1
        elif [ -z "$dir" -o "$dir" == "/" -o "$dir" == "/proc" -o "$dir" == "/dev" ]; then
                (>&2 err "Illegal strings")
                return 1
        elif [ ! -d "$dir" ]; then
                mkdir -p "$dir" 2>/dev/null
                if [ $? -ne 0 ]; then
                        (>&2 err "Cannot create directory ‘$dir’: Permission denied")
                        return 1
                fi
        elif [ ! -w "$dir" ]; then
                (>&2 err "Cannot write directory ‘$dir’: Permission denied")
                return 1
        elif [ -n "$(ls $dir)" ]; then
				# 判断是否已经安装过
				is_installed=$(grep ":$dir:" $DATAXONE_LOCAL_CONFIG_FILE)
				if [ -z "$is_installed" ]; then
                	(>&2 err "Data Dictionary initialization failed, Non empty directory ‘$dir’")
                	return 1
				fi
        fi
}

checkPort(){
	# 检查文件
	info "Checking bind $1..."
	bind_port=$(egrep "(:$1:|:$1$)" $DATAXONE_LOCAL_PORT_FILE)
	if [ -n "$bind_port" ]; then
		dataxone_base_0=$(echo $bind_port | awk -F: '{print $1}')
		(>&2 err "Checking bind $1 OK, but port already in use on DATAXONE_BASE=$dataxone_base_0")
		abort
	else
		info "Checking bind $1 OK."
	fi

	info "Checking listen $1..."
	ps_check_result=$(netstat -anp 2>/dev/null | egrep ":$1 .*LISTEN" | awk '{print $NF}') 
	
	if [ -n "$ps_check_result" ]; then
		(>&2 err "Checking listen $1 OK, but address already in use: $ps_check_result")
        abort
	else
        info "Checking listen $1 OK."
	fi
}

handleUserDirectory $ARGV_PARAM_DATAXONE_INSTALL_DIR
[ $? -ne 0 ] && abort || mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR


dataxone_config=$(grep ":$dir:" $DATAXONE_LOCAL_CONFIG_FILE)
if [ -n "$dataxone_config" ]; then
	info "Dataxone Patch upgrade for ‘$ARGV_PARAM_DATAXONE_INSTALL_DIR’"
	DATAXONE_VERSION=$(echo "$dataxone_config" | awk -F: '{print $3}')
	# LOAD_BALANCE=$(echo "$dataxone_config" | awk -F: '{print $4}')
	# AUTO_START=$(echo "$dataxone_config" | awk -F: '{print $5}')
	# echo $DATAXONE_VERSION
	# echo $LOAD_BALANCE
	# echo $AUTO_START
	# info "Current Version: $DATAXONE_VERSION"

	
	eval "$(grep ^PRETTY_NAME /etc/os-release)"
	echo "
$(basename $0)	$version
DataXone interim Installer version $version
copyright (c) 2024, dsgdata.com.  All rights reserved.

Dataxone base      	 : $ARGV_PARAM_DATAXONE_INSTALL_DIR
Dataxone version         : $DATAXONE_VERSION
Installed Option modules :$option_modules
Inventory location 	 : $dataxone_inventory
Patch directory	   	 : $patchset_dir
Log file location  	 : $logfile

--------------------------------------------------------------------------------

Local Machine Information::
Hostname: $(uname -n)
Platform description: $(uname -s) $(uname -a | awk '{print $3}')
Pretty name: $PRETTY_NAME
"

	dataxone_port=$(grep "^$dir:" $DATAXONE_LOCAL_PORT_FILE)

	for i in `seq 1 $(head -1 $DATAXONE_LOCAL_PORT_FILE | tr ':' '\n' | wc -l)`
	do
		value=$(echo "$dataxone_port" | awk -F: -v i=$i '{print $i}')
		if [ $i -eq 1 ]; then
			printf "\nChecking Listen for %s:\n" "$value"
			continue
		fi
		app=$(head -1 $DATAXONE_LOCAL_PORT_FILE | awk -F: -v i=$i '{print $i}')
		printf "%d - %5s (%s)\n" `expr $i - 1` "$value" "$app"
	done
	echo

	tomcat_port=$(echo "$dataxone_port" | awk -F: '{print $2}')
	dataXoneTomcat_port=$(echo "$dataxone_port" | awk -F: '{print $3}')
	regd_port=$(echo "$dataxone_port" | awk -F: '{print $4}')
	bigdataQuery_Engine_port=$(echo "$dataxone_port" | awk -F: '{print $5}')
	query_db_engine_port=$(echo "$dataxone_port" | awk -F: '{print $6}')
	Manager_Node_Engine_port=$(echo "$dataxone_port" | awk -F: '{print $7}')
	yxad_mysql_port=$(echo "$dataxone_port" | awk -F: '{print $8}')

else
	info "First installation for ‘$ARGV_PARAM_DATAXONE_INSTALL_DIR’"

	tomcat_port=${tomcat_port:-8080}
	dataXoneTomcat_port=${dataXoneTomcat_port:-8081}
	regd_port=${regd_port:-6000}
	bigdataQuery_Engine_port=${bigdataQuery_Engine_port:-8880}
	query_db_engine_port=${query_db_engine_port:-9011}
	Manager_Node_Engine_port=${Manager_Node_Engine_port:-9998}
	yxad_mysql_port=${yxad_mysql_port:-65534}

	checkPort $tomcat_port
	checkPort $dataXoneTomcat_port
	checkPort $regd_port
	checkPort $bigdataQuery_Engine_port
	checkPort $query_db_engine_port
	checkPort $Manager_Node_Engine_port
	checkPort $yxad_mysql_port
fi




exit 0






#set -e

rm -rf $TMP_DIR/*





#### confirm install dir




info "Entry directory ‘$dir’"

info "Extract file from package $ARGV_PARAM_FILE ..."
tar -zxf $ARGV_PARAM_FILE -C $SCRIPT_HOME/tmp
package_dir=$(cd $SCRIPT_HOME/tmp/*ataXone* ; pwd)
info "Extract file from package $ARGV_PARAM_FILE OK."

#####
# yxad.mysql
#####
tar -zxf $package_dir/yxad.mysql.tar.gz -C $ARGV_PARAM_DATAXONE_INSTALL_DIR
echo "export YXAD_HOST=127.0.0.1" >> $ARGV_PARAM_DATAXONE_INSTALL_DIR/yxad.mysql/scripts/profile


#####
# regd
#####
tar -zxf $package_dir/regd.tar.gz -C $ARGV_PARAM_DATAXONE_INSTALL_DIR
echo "export DBPS_LICENSE_IP=${ARGV_PARAM_DATAXONE_APP_NODES/,/:}" >> $ARGV_PARAM_DATAXONE_INSTALL_DIR/regd/scripts/profile

#####
# engineDsg
#####
tar -zxf $package_dir/engineDsg.tar.gz -C $ARGV_PARAM_DATAXONE_INSTALL_DIR

#####
# pwdcrypt
#####
tar -zxf $package_dir/pwdcrypt.tar.gz -C $ARGV_PARAM_DATAXONE_INSTALL_DIR
chmod +x $ARGV_PARAM_DATAXONE_INSTALL_DIR/pwdcrypt/pwdcrypt.sh
EN_ARGV_PARAM_DATAXONE_DB_USER_PASSWORD=$(cd $ARGV_PARAM_DATAXONE_INSTALL_DIR/pwdcrypt && ./pwdcrypt.sh $ARGV_PARAM_DATAXONE_DB_USER_PASSWORD | grep -v ^$ | tail -1)


#####
# !!!!!!!!!!!!!!补充Manager密码加密脚本
####

#####
# tomcat
#####
tar -zxf $package_dir/apache-tomcat-9.*.tar.gz -C $ARGV_PARAM_DATAXONE_INSTALL_DIR
mv $ARGV_PARAM_DATAXONE_INSTALL_DIR/apache-tomcat-* $ARGV_PARAM_DATAXONE_INSTALL_DIR/tomcat
cp -R $ARGV_PARAM_DATAXONE_INSTALL_DIR/tomcat $ARGV_PARAM_DATAXONE_INSTALL_DIR/dataXoneTomcat

WEBAPPS=$ARGV_PARAM_DATAXONE_INSTALL_DIR/www/webapps
mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR/www/ROOT
mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR/www/webapps

ARGV_PARAM_TOMCAT_PORT=${ARGV_PARAM_TOMCAT_PORT:-8080}
ARGV_PARAM_DATAXONE_TOMCAT_PORT=${ARGV_PARAM_DATAXONE_TOMCAT_PORT:-8081}

apps(){
	case $1 in
        tomcat)	
		[ -z "$ARGV_PARAM_WEBAPPS" ] && echo "1" || echo $ARGV_PARAM_WEBAPPS | tr ',' '\n' | grep "$2"
		;;
	dataXoneTomcat)
		[ -z "$ARGV_PARAM_DATAXONE_WEBAPPS" ] && echo "1" || echo "$ARGV_PARAM_DATAXONE_WEBAPPS" | tr ',' '\n' | grep "$2"
		;;
	esac
}

configTomcat(){
	conf=$ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/conf/server.xml
	sed -i "s/SHUTDOWN/$(uuidgen | awk '{print substr($0,0,8)}')/" $conf
	sed -i 's/Connector port="8080" protocol="HTTP\/1.1"/& allowTrace="false" xpoweredBy="false" server="AppServer1.0"/' $conf
	sed -i 's#appBase="webapps"#appBase="'$ARGV_PARAM_DATAXONE_INSTALL_DIR/www/ROOT'"#' $conf
	sed -i 's/autoDeploy="true"/autoDeploy="false" deployOnStartup="false" deployXML="false"/' $conf
	sed -i '/unpackWARs="true"/a \\n\t<Value className="org.apache.catalina.values.ErrorReportValue" showReport="false" showServerInfo="false" />' $conf

	rm -rf $ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/webapps/* 2>/dev/null

	
	case $1 in
	tomcat)
		[ -n "$(apps $1 DSGWEB)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/DSGWEB" docBase="'$WEBAPPS'/DSGWEB" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf
		[ -n "$(apps $1 sbt_xcmp)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/sbt_xcmp" docBase="'$WEBAPPS'/sbt_xcmp" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf
		[ -n "$(apps $1 permission)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/permission" docBase="'$WEBAPPS'/permission" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf
		[ -n "$(apps $1 dataXone_theme_dsg_project-0.0.1)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/dataXone_theme_dsg_project-0.0.1" docBase="'$WEBAPPS'/dataXone_theme_dsg_project-0.0.1" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf
		[ -n "$(apps $1 shell_mon)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/shell_mon" docBase="'$WEBAPPS'/shell_mon" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf

		sed -i "s/8080/${ARGV_PARAM_TOMCAT_PORT}/" $conf
		mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/secretKey
		sed -i '/<\/Context>/i\    <Resources cachingAllowed="true" cacheMaxSize="1000000"/>' $ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/conf/context.xml

		cat > $ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/bin/setenv.sh << EOF
CATALINA_OPTS="\${CATALINA_OPTS} -Djava.awt.headless=true"
JAVA_OPTS="\${JAVA_OPTS} -Xms256m -Xmx2048m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=1024m -Xss2m"

EOF
			

		;;
	dataXoneTomcat)
		[ -n "$(apps $1 autoMaticEngineBoot-1.0.0)" ] && sed -i '/org.apache.catalina.values.ErrorReportValue/a\\n\t<Context path="/autoMaticEngineBoot-1.0.0" docBase="'$WEBAPPS'/autoMaticEngineBoot-1.0.0" reloadable="false" crossContext="false" privileged="false" allowLinking="false" sessionCookiePathUsesTrailingSlash="true" />' $conf
		sed -i "s/8080/${ARGV_PARAM_DATAXONE_TOMCAT_PORT}/" $conf
		sed -i "s/8005/8015/" $conf
		cat > $ARGV_PARAM_DATAXONE_INSTALL_DIR/$1/bin/setenv.sh << EOF
CATALINA_OPTS="\${CATALINA_OPTS} -Djava.awt.headless=true"
JAVA_OPTS="\${JAVA_OPTS} -Xms3072m -Xmx4096m -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=1024m -Xss2m"

EOF

		;;
	esac



}

configTomcat tomcat
configTomcat dataXoneTomcat


#### depoy webapps
depoyWebapps(){
	> $TMP_DIR/apps.sql

	if [ -n "$(apps tomcat permission)" ]; then

		db_name=permission_dsg

		cat > $TMP_DIR/permission.application.properties <<EOF


spring.datasource.url = jdbc:mysql://${ARGV_PARAM_DATAXONE_DB_HOST:-127.0.0.1}:${ARGV_PARAM_DATAXONE_DB_PORT:-3306}/$db_name?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username = $ARGV_PARAM_DATAXONE_DB_USER
spring.datasource.password = ENC($EN_ARGV_PARAM_DATAXONE_DB_USER_PASSWORD)
dataXone.dbName = automation_dsg_db_new
socket.ip = 127.0.0.1
xxl.job.admin.addresses = http://127.0.0.1:${ARGV_PARAM_TOMCAT_PORT}/permission
xxl.job.executor.ip = 127.0.0.1
module.path = http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_TOMCAT_PORT}/permission
file.urlforpartner = $ARGV_PARAM_DATAXONE_INSTALL_DIR/tomcat/secretKey
logging.level.com.dsg.soft.dao = info


EOF

		unzip -oq $package_dir/permission.war* -d $WEBAPPS/permission
		$SCRIPT_HOME/config_master.sh $WEBAPPS/permission/WEB-INF/classes/application.properties $TMP_DIR/permission.application.properties
		sed -i 's/<root level=\"all\">/<root level=\"info\">/g' $WEBAPPS/permission/WEB-INF/classes/log4j2.xml 2>/dev/null

		info "App permission configuration OK."

		sql_file=$(ls $package_dir/ | egrep '^permission_dsg.*\.sql$')
		sed -i "1 iuse ${db_name};" $package_dir/$sql_file
		echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql
		
		egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
		echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql
	
	fi

	##########################################################################################

	if [ -n "$(apps tomcat dataXone_theme_dsg_project-0.0.1)" ]; then

		db_name=automated_screen_theme_report_db
		
		cat > $TMP_DIR/dataXone_theme_dsg_project.application.properties <<EOF

module.c.mysql.ip_address = $ARGV_PARAM_DATAXONE_DB_HOST
module.c.mysql.port = $ARGV_PARAM_DATAXONE_DB_PORT
spring.datasource.url = jdbc:mysql://${ARGV_PARAM_DATAXONE_DB_HOST:-127.0.0.1}:${ARGV_PARAM_DATAXONE_DB_PORT:-3306}/$db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username = $ARGV_PARAM_DATAXONE_DB_USER
spring.datasource.password = ENC($EN_ARGV_PARAM_DATAXONE_DB_USER_PASSWORD)
permission.dbName = permission_dsg
dataXone.dbName = automation_dsg_db_new
module.path=http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_TOMCAT_PORT}/dataXone_theme_dsg_project-0.0.1

##### nothing to do....
#pic.host = 127.0.0.1
#pic.username = dataxone
#pic.password = dataxone
#pic.urlforpartner = $WEBAPPS/DSGWEB/static/img/screen/imgScreen


EOF

		cat > $TMP_DIR/log4j.properties <<EOF
log4j.rootLogger = trace,INFO,stdout,info,warn,error

EOF

		unzip -oq $package_dir/dataXone_theme_dsg_project-0.0.1.war* -d $WEBAPPS/dataXone_theme_dsg_project-0.0.1
		$SCRIPT_HOME/config_master.sh $WEBAPPS/dataXone_theme_dsg_project-0.0.1/WEB-INF/classes/application.properties $TMP_DIR/dataXone_theme_dsg_project.application.properties

		$SCRIPT_HOME/config_master.sh $WEBAPPS/dataXone_theme_dsg_project-0.0.1/WEB-INF/classes/log4j.properties $TMP_DIR/log4j.properties

		info "App dataXone_theme_dsg_project-0.0.1 configuration OK."
		
		sql_file=$(ls $package_dir/ | egrep '^automated_screen_theme_report_db.*\.sql$')
		sed -i "1 iuse ${db_name};" $package_dir/$sql_file
		echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql

		egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
                echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql


	fi


	if [ -n "$(apps tomcat shell_mon)" ]; then

		unzip -oq $package_dir/shell_mon.war* -d $WEBAPPS/shell_mon
		\cp $TMP_DIR/globalParams.json $WEBAPPS/shell_mon/static/globalParams.json 2>/dev/null

		info "App WEB-UI shell_mon configuration OK."
	fi

	if [ -n "$(apps tomcat sbt_xcmp)" ]; then

		db_name=data_xcmp

		cat > $TMP_DIR/sbt_xcmp.application.properties <<EOF

module.c.mysql.ip_address = $ARGV_PARAM_DATAXONE_DB_HOST
module.c.mysql.port = $ARGV_PARAM_DATAXONE_DB_PORT
module.c.mysql.db_name = data_xcmp
module.c.mysql.username = $ARGV_PARAM_DATAXONE_DB_USER
module.c.mysql.password = $ARGV_PARAM_DATAXONE_DB_USER_PASSWORD

spring.datasource.url = jdbc:mysql://$ARGV_PARAM_DATAXONE_DB_HOST:$ARGV_PARAM_DATAXONE_DB_PORT/$db_name?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username = $ARGV_PARAM_DATAXONE_DB_USER
spring.datasource.password = ENC($EN_ARGV_PARAM_DATAXONE_DB_USER_PASSWORD)
module.path = http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_TOMCAT_PORT}/sbt_xcmp

EOF
		
		unzip -oq $package_dir/sbt_xcmp.war* -d $WEBAPPS/sbt_xcmp
		$SCRIPT_HOME/config_master.sh $WEBAPPS/sbt_xcmp/WEB-INF/classes/application.properties $TMP_DIR/sbt_xcmp.application.properties

		info "App sbt_xcmp configuration OK."

		sql_file=$(ls $package_dir/ | egrep '^data_xcmp.*\.sql$')
		sed -i "1 iuse ${db_name};" $package_dir/$sql_file
		echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql

		egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
                echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql

	fi
		

	if [ -n "$(apps dataXoneTomcat autoMaticEngineBoot-1.0.0)" ]; then

		info "App autoMaticEngineBoot configuration ..."

		db_name=automation_dsg_db_new

		cat > $TMP_DIR/autoMaticEngineBoot.application.properties <<EOF

module.c.mysql.ip_address = $ARGV_PARAM_DATAXONE_DB_HOST
module.c.mysql.port = $ARGV_PARAM_DATAXONE_DB_PORT
module.yxad.service.ip_address = $ARGV_PARAM_DATAXONE_PORTAL
module.yxad.service.port = 65534
spring.datasource.url = jdbc:mysql://$ARGV_PARAM_DATAXONE_DB_HOST:${ARGV_PARAM_DATAXONE_DB_PORT:-3306}/$db_name?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
spring.datasource.username = $ARGV_PARAM_DATAXONE_DB_USER
spring.datasource.password = ENC($EN_ARGV_PARAM_DATAXONE_DB_USER_PASSWORD)
this.engine.package.path = $ARGV_PARAM_DATAXONE_INSTALL_DIR/engineDsg
module.permission_db = permission_dsg
regd.path = $ARGV_PARAM_DATAXONE_INSTALL_DIR/regd
kerberosHiveSocketQueryIp = $ARGV_PARAM_DATAXONE_PORTAL
module.path = http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_DATAXONE_TOMCAT_PORT}/autoMaticEngineBoot-1.0.0
DBPS_REGD = $ARGV_PARAM_DATAXONE_PORTAL:6000
dataHub.url = http://$ARGV_PARAM_DATAXONE_PORTAL:8880

### nothing to do...
# ftp_collection_hostName = 
# ftp_collection_hostIp = 
# ftp_collection_userName =
# ftp_collection_password =


EOF
		
		unzip -oq $package_dir/autoMaticEngineBoot-1.0.0.war* -d $WEBAPPS/autoMaticEngineBoot-1.0.0
		$SCRIPT_HOME/config_master.sh $WEBAPPS/autoMaticEngineBoot-1.0.0/WEB-INF/classes/application.properties $TMP_DIR/autoMaticEngineBoot.application.properties
		
		sed -i 's/<root level=\"all\">/<root level=\"info\">/g' $WEBAPPS/autoMaticEngineBoot-1.0.0/WEB-INF/classes/log4j2.xml
		sed -i 's/<Logger name=\"com.neo.dao\"/<Logger name=\"com.neo.dao\" level=\"info\" additivity=\"false\">/g' $WEBAPPS/autoMaticEngineBoot-1.0.0/WEB-INF/classes/log4j2.xml

		info "App autoMaticEngineBoot configuration OK"
		
		sql_file=$(ls $package_dir/ | egrep '^automation_dsg_db_new.*\.sql$')
		sed -i "1 iuse ${db_name};" $package_dir/$sql_file
		echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql

		egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
                echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql

	fi
}


depoyWebapps


installOracleInstantclient(){

	info "Install oracle instantclient ..."
	
	oracle_base=${ARGV_PARAM_HOST_ORACLE_CLIENT_BASE:-/usr/oracle}
	mkdir -p $oracle_base
	
	unzip -oq $SCRIPT_HOME/stage/instantclient-basic-linux.x64-*.*.0.0.0dbru.zip 	-d $oracle_base
	unzip -oq $SCRIPT_HOME/stage/instantclient-sqlplus-linux.x64-*.*.0.0.0dbru.zip	-d $oracle_base

	cd $oracle_base
	instantclient_version_path=$(ls | egrep '^instantclient_[0-9]{1,}_[0-9]{1,}' | tail -1)	
	rm -rf instantclient 2>/dev/null
	ln -s $instantclient_version_path instantclient 2>/dev/null

	oracle_home=$oracle_base/instantclient
	cd $oracle_home
	mkdir -p _lib64 2>/dev/null
	cp lib* _lib64  2>/dev/null
	mv _lib64 lib64 2>/dev/null
	ln -s lib64 lib 2>/dev/null

	#client_version=$(echo $instantclient_version_path | sed -e 's/instantclient_//' -e 's/_/./' -e 's/$/.0/')
	
	#oracle_home=$oracle_base/product/$client_version/db_1
	#mkdir -p $oracle_home

	#unzip -oq $SCRIPT_HOME/stage/instantclient-basic-linux.x64-*.*.0.0.0dbru.zip    -d $oracle_home
	#mv $oracle_home/$instantclient_version_path $oracle_home/lib
	#rm -rf $oracle_home/lib/network

	#unzip -oq $SCRIPT_HOME/stage/instantclient-sqlplus-linux.x64-*.*.0.0.0dbru.zip  -d $oracle_home
	#mv $oracle_home/$instantclient_version_path $oracle_home/bin

	#ln -s $oracle_base/$instantclient_version_path/network $oracle_home/network

	tns_admin=$oracle_home/network/admin
	mkdir -p $tns_admin
	touch $tns_admin/tnsnames.ora

	chown -R $ARGV_PARAM_HOST_USER:$ARGV_PARAM_HOST_USER $oracle_base

	info "Install oracle instantclient OK."

	
	cat > /etc/profile.d/oracle_instantclient.sh <<EOF

export ORACLE_HOME=$oracle_home
export TNS_ADMIN=$tns_admin
#export PATH=\$ORACLE_HOME/bin:\$PATH
export PATH=\$ORACLE_HOME:\$PATH
#export LD_LIBRARY_PATH=\$ORACLE_HOME/bin:\$ORACLE_HOME/lib:\$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=\$ORACLE_HOME:\$LD_LIBRARY_PATH


EOF

}

[ -z "$ARGV_PARAM_HOST_SKIP_INSTALL_ORACLE_CLIENT" ] && installOracleInstantclient


installManager4TaskHa(){
	#########################
	info "Install dataxone Manager (Replication Task HA Component) ..."

	db_name=dataxone_bigdatas_db

	cd $ARGV_PARAM_DATAXONE_INSTALL_DIR/
	tar -zxf $package_dir/Manager_Node_Engine.tar.gz
	cd $ARGV_PARAM_DATAXONE_INSTALL_DIR/Manager_Node_Engine*/
	mkdir -p logs
	ln -s logs log

	# 
	cat > $TMP_DIR/manager.config.properties <<EOF

SERVERPORT=${ARGV_PARAM_DATAXONE_MRG_PORT:-9998}
MANAGER_CLUSTER_ADDRESS=${ARGV_PARAM_DATAXONE_APP_NODES}
ZOOKEEPER_CLUSTER_ADDRESS=${ARGV_PARAM_DATAXONE_ZOOKEEPER_SERVICES}
DATAXONE_HOST_IP=${ARGV_PARAM_DATAXONE_PORTAL}
DATAXONE_HOST_PORT=${ARGV_PARAM_DATAXONE_TOMCAT_PORT:-8081}

EOF
	cat > $TMP_DIR/manager.jdbc.properties <<EOF

spring.datasource.url=jdbc:mysql://$ARGV_PARAM_DATAXONE_DB_HOST:${ARGV_PARAM_DATAXONE_DB_PORT:-3306}/$db_name?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=$ARGV_PARAM_DATAXONE_DB_USER
spring.datasource.password=$ARGV_PARAM_DATAXONE_DB_USER_PASSWORD

EOF
	$SCRIPT_HOME/config_master.sh config/config.properties $TMP_DIR/manager.config.properties
	$SCRIPT_HOME/config_master.sh config/module/dataBase/properties/jdbc.properties $TMP_DIR/manager.jdbc.properties

	cat > start.sh <<EOF
export JAVA_HOME=\${JAVA_HOME:-$ARGV_PARAM_HOST_JAVA_HOME}
export PATH=\$JAVA_HOME/bin:\$PATH
cd $PWD
./startManagerNodeEngine.sh start > $PWD/logs/info.log 2>&1 &

EOF
	chmod +x start.sh
	#chown -R $ARGV_PARAM_HOST_USER:$ARGV_PARAM_HOST_USER start.sh
	#chown -R $ARGV_PARAM_HOST_USER:$ARGV_PARAM_HOST_USER config/config.properties
	#chown -R $ARGV_PARAM_HOST_USER:$ARGV_PARAM_HOST_USER config/module/dataBase/properties/jdbc.properties

	info "Install dataxone Manager (Replication Task HA Component) OK"

	sql_file=$(ls $package_dir/ | egrep '^dataxone_bigdatas_db.*\.sql$')
	sed -i "1 iuse ${db_name};" $package_dir/$sql_file
	echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql

	egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
	echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql

}


[ -z "$ARGV_PARAM_DATAXONE_SKIP_HA" ] && installManager4TaskHa


installDataxoneLargeData(){
	#
	info "Install dataxone bigdataQuery_Engine... "
	mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR/DataXone_Query_Engine
	cd $ARGV_PARAM_DATAXONE_INSTALL_DIR/DataXone_Query_Engine
	tar -zxf $package_dir/bigdataQuery_Engine.tar.gz
	cd bigdataQuery_Engine*/
	mkdir -p logs  2>/dev/null
        ln -s logs log 2>/dev/null
	
	cat > start.sh <<EOF
export JAVA_HOME=\${JAVA_HOME:-$ARGV_PARAM_HOST_JAVA_HOME}
export PATH=\$JAVA_HOME/bin:\$PATH
cd $PWD
./startJddmSdkByPgmdEngine.sh start startJddmDSGBigDataQueryEngine query > logs/info.log 2>&1 &

EOF

	chmod +x start.sh
	info "Install dataxone bigdataQuery_Engine OK "


}

installDataxoneLargeData


installOther(){
        #
        info "Install dataxone component for GaussDB... "
        mkdir -p $ARGV_PARAM_DATAXONE_INSTALL_DIR/DataXone_Query_Engine
        cd $ARGV_PARAM_DATAXONE_INSTALL_DIR/DataXone_Query_Engine
        tar -zxf $package_dir/query_db_engine.tar.gz
        cd query_db_engine*/
        mkdir -p logs  2>/dev/null
        ln -s logs log 2>/dev/null

	cat > $TMP_DIR/other.jdbc.properties <<EOF

mysqlmonitor.jdbc.url=jdbc:mysql://$ARGV_PARAM_DATAXONE_DB_HOST:${ARGV_PARAM_DATAXONE_DB_PORT:-3306}/automation_dsg_db_new?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false
mysqlmonitor.jdbc.username=$ARGV_PARAM_DATAXONE_DB_USER
mysqlmonitor.jdbc.password=$ARGV_PARAM_DATAXONE_DB_USER_PASSWORD

EOF

	$SCRIPT_HOME/config_master.sh config/module/properties/jdbc.properties $TMP_DIR/other.jdbc.properties

        cat > start.sh <<EOF
export JAVA_HOME=\${JAVA_HOME:-$ARGV_PARAM_HOST_JAVA_HOME}
export PATH=\$JAVA_HOME/bin:\$PATH
cd $PWD
./startQueryDataXoneEngine.sh start opengauss 9011 > logs/info.log 2>&1 &

EOF

	chmod +x start.sh

	cat > $TMP_DIR/init999.sql <<EOSQL
UPDATE automation_dsg_db_new.op_big_database_engine_info_tb SET engine_path='$ARGV_PARAM_DATAXONE_INSTALL_DIR/DataXone_Query_Engine';
INSERT INTO automation_dsg_db_new.rel_big_database_cluster_engine_status_tb (engine_address, engine_port, engine_status, engine_alias, ex_msg) VALUES ('$ARGV_PARAM_DATAXONE_PORTAL', 9011, 'start', 'opengauss', NULL);
EOSQL

        info "Install dataxone query_db_engine OK "
}

installOther


initDatabase(){
	
	db_name=dataxone_pmon
	sql_file=$(ls $package_dir/ | egrep '^dataxone_pmon.*\.sql$')
	sed -i "1 iuse ${db_name};" $package_dir/$sql_file
	echo -e "drop database if exists $db_name; \ncreate database $db_name; \nsource $package_dir/$sql_file" >> $TMP_DIR/apps.sql
	egrep " $db_name." $package_dir/init_dataxone.sql > $TMP_DIR/init_${db_name}.sql
	echo "source $TMP_DIR/init_${db_name}.sql" >> $TMP_DIR/apps.sql


	info "Init database..."
	export MYSQL_PWD="${ARGV_PARAM_DATAXONE_DB_USER_PASSWORD}"

	mysql -h${ARGV_PARAM_DATAXONE_DB_HOST} -u${ARGV_PARAM_DATAXONE_DB_USER} -P${ARGV_PARAM_DATAXONE_DB_PORT:-3306} <<EOSQL
SOURCE $TMP_DIR/apps.sql;
SOURCE $TMP_DIR/init999.sql;
EOSQL

	info "Init database OK"

}

[ -z "$ARGV_PARAM_DATAXONE_SKIP_INIT_DB" ] && initDatabase



getVersion(){
	export MYSQL_PWD="${ARGV_PARAM_DATAXONE_DB_USER_PASSWORD}"
	mysql -s -h${ARGV_PARAM_DATAXONE_DB_HOST} -u${ARGV_PARAM_DATAXONE_DB_USER} -P${ARGV_PARAM_DATAXONE_DB_PORT:-3306} > $TMP_DIR/version.out <<EOSQL
select version_code from automation_dsg_db_new.sys_version_record_info_tb 
where id in (select max(id) from automation_dsg_db_new.sys_version_record_info_tb);
exit
EOSQL
	cat $TMP_DIR/version.out

}


depoy_webUI(){

	##########################################################################################

        if [ -n "$(apps tomcat DSGWEB)" ]; then
                unzip -oq $package_dir/DSGWEB.war* -d $WEBAPPS/DSGWEB
                conf=$WEBAPPS/DSGWEB/static/globalParams.json

                ### write py file
                cat > $TMP_DIR/0.py <<EOF
#! /usr/bin/env python
# coding: utf-8

import json
import sys
reload(sys)

sys.setdefaultencoding('utf-8')

with open('$conf') as f:
        data=json.load(f)

data['communicationAddressTest'] = "http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_TOMCAT_PORT}/permission"
data['guoshui'] = "http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_DATAXONE_TOMCAT_PORT}/autoMaticEngineBoot-1.0.0"
data['communicationTimeout'] = 100000
data['alarmBtnStat'] = "dataxone"
data['versionControlMenu'] = "new"
data['disasterWarning'] = True
data['projectName'] = "DataXone交换集成管理平台"
data['version'] = "DataXone-$(getVersion)"
data['showAddParam'] = False
data['nationalTax'] = True
data['BigScreenDisplayOfHomePage'] = False
data['customTimer']['isOpen'] = False
data['bigScreenMonitorShow'] = False


print(json.dumps(data, indent=4, ensure_ascii=False))

EOF
                python $TMP_DIR/0.py > $TMP_DIR/globalParams.json
                \cp $TMP_DIR/globalParams.json $conf 2>/dev/null

                info "App WEB-UI configuration OK."
        fi

}

depoy_webUI


# finish & run chown...
chown -R $ARGV_PARAM_HOST_USER:$ARGV_PARAM_HOST_USER $ARGV_PARAM_DATAXONE_INSTALL_DIR

## root
cat >> /etc/profile <<EOF

export PS1='[\[\e[1;35m\]\u@\h\[\e[0m\]: \[\e[33m\]\w\[\e[0m\]]\\$ '
EOF


### 
\cp $SCRIPT_HOME/dataxone-sentinel.sh $ARGV_PARAM_DATAXONE_INSTALL_DIR/
sed -i "s/8080/${ARGV_PARAM_TOMCAT_PORT}/" $ARGV_PARAM_DATAXONE_INSTALL_DIR/dataxone-sentinel.sh
sed -i "s/8081/${ARGV_PARAM_DATAXONE_TOMCAT_PORT}/" $ARGV_PARAM_DATAXONE_INSTALL_DIR/dataxone-sentinel.sh
sed -i "s#DATAXONE_BASE=/data/dataxone#DATAXONE_BASE=$ARGV_PARAM_DATAXONE_INSTALL_DIR#" $ARGV_PARAM_DATAXONE_INSTALL_DIR/dataxone-sentinel.sh

sh $ARGV_PARAM_DATAXONE_INSTALL_DIR/dataxone-sentinel.sh



echo -e "\n\n#### \x1b[33mDataXone install successfully.\x1b[0m ####\nWelcome to DataXone, version: $(getVersion)\n  Web administration GUI: http://$ARGV_PARAM_DATAXONE_PORTAL:${ARGV_PARAM_TOMCAT_PORT}/DSGWEB\n  Username: dataxone\n  Password: 123456\nbye.\n\n"





########################################################################
# 不允许修改
chmod -w $DATAXONE_LOCAL_CONFIG_FILE 
chmod -w $DATAXONE_LOCAL_PORT_FILE 
chmod -w $DATAXONE_LOCAL_WEBAPPS_FILE 